<template>
    <div class="tree_icon">
        <el-icon style="float: right" @click="closeMedicalRoomClick">
            <close />
        </el-icon>
    </div>
    <br /><br />
    <div style="text-align: center">
        <el-input v-model="compState.filterText" :placeholder="placeholderFormatFun()"
            style="width: 90%; margin-bottom: 10px" :prefix-icon="Search" />
    </div>
    <el-scrollbar height="400px">
        <el-tree ref="treeRef" class="filter-tree" :key="props.isOnlinePerson"
            :props="{ label: 'Name', children: 'Children', isLeaf: 'Leaf' }" node-key="Code" :load="loadNode" lazy
            @node-click="handlemedicalRoomPointNodeClick" :filter-node-method="filterNode">
            <template #default="{ data }">
                <div class="online-status-clas" v-show="data.IsUser"
                    :style="{ 'background-color': data.OnLine == true ? '#67c23a' : '#909399' }"></div>
                <span style="font-size: 15px;">
                    {{ data.Name }}
                </span>
            </template>
        </el-tree>
    </el-scrollbar>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { setupMapState } from '../../../store/utils/hook'
import { QueryUserList } from '../../../network/hosVenAmbuListData'
import { Search } from '@element-plus/icons-vue'

const props = defineProps({
    data: {
        type: Array,
        default: () => []
    },
    userType: {
        type: Number,
        default: () => 0
    },
    isOnlinePerson: {
        type: Number,
        default: () => 1
    }
})
const emit = defineEmits(['closeUserInfoEmit', "userInfoNodeEmit"])
const treeRef = ref()
const compState = reactive({
    filterText: "",
})
const { ACVCodesVx } = setupMapState("app", ["ACVCodesVx"])
// 监听输入框
watch(() => compState.filterText, (newV) => {
    // console.log(newV)
    treeRef.value.filter(newV)
})
// 过滤节点数据
const filterNode = (value, data) => {
    if (!value) return true;
    return data.Name.includes(value);
};
const requestData = async (activityCode, cityCode, venueCode, userType, type, isOnlinePerson) => {
    let res = await QueryUserList({
        activityCode: activityCode,
        cityCode: cityCode,
        venueCode: venueCode,
        userType: userType,
        type: type,
        onLine: isOnlinePerson
    })
    return res
}
const loadNode = async (node, resolve) => {
    // console.log("node", node)
    // console.log("props", props)
    // console.log("ACVCodesVx", ACVCodesVx)
    let acvCodes = ACVCodesVx.value
    // console.log(acvCodes)
    let res = []
    if (node.level === 0) {
        res = await requestData(acvCodes.activityCode,
            acvCodes.cityCode,
            acvCodes.venueCode,
            props.userType,
            0,
            props.isOnlinePerson
        )
        // console.log(res)
        resolve(res)
    }
    if (node.level === 1) {//点击城市
        res = await requestData(acvCodes.activityCode,
            node.data.Code,
            acvCodes.venueCode,
            props.userType,
            1,
            props.isOnlinePerson
        )
        treeRef.value.updateKeyChildren(node.data.Code, res)
        resolve(res)
    }
    if (node.level === 2) {//点击场馆
        res = await requestData(acvCodes.activityCode,
            node.data.ParentID,
            node.data.Code,
            props.userType,
            2,
            props.isOnlinePerson
        )
        treeRef.value.updateKeyChildren(node.data.Code, res)
        resolve(res)
    }
    if (node.level === 3) {//点击人员
        console.log("点击了人员leve--3")
    }
}
const placeholderFormatFun = () => {
    let placeholder = ""
    if (props.userType == 5) {//医生
        placeholder = '医生搜索'
    } else {//护士
        placeholder = '护士搜索'
    }
    return placeholder
}
const handlemedicalRoomPointNodeClick = (node) => {
    // console.log(node)
    if (node.IsUser)
        emit("userInfoNodeEmit", node)
}
const closeMedicalRoomClick = () => {
    compState.filterText = ""
    emit('closeUserInfoEmit', { type: props.userType })
}
</script>

<style scoped>
.online-status-clas {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 5px;
}

.tree_icon {
    margin: 5px;
}

.tree_icon:hover {
    color: #758bfd;
    cursor: pointer;
}
</style>